package com.hqyj.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.dao.SupplierMapper;
import com.hqyj.pojo.Supplier;
import com.hqyj.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class SupplierServiceImpl implements SupplierService {

    //注入mapper
    @Autowired(required = false)
    SupplierMapper supplierMapper;


    @Override
    public HashMap<String, Object> select(Supplier s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //定义分页对象
        Page<Supplier> page = new Page<>(s.getPage(), s.getRow());

        //条件构造器
        QueryWrapper<Supplier> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(s.getSupplierId()!=null,"supplier_id",s.getSupplierId());
        queryWrapper.like(s.getSupplierName()!=null&&!s.getSupplierName().equals(""),"supplier_name",s.getSupplierName());
        Page<Supplier> sList = supplierMapper.selectPage(page, queryWrapper);
        map.put("list",sList.getRecords());
        //总页数
        map.put("totalPage",sList.getPages());
        //总条数
        map.put("total",sList.getTotal());
        //当前页
        map.put("curPage",s.getPage());
        if(s.getPage()==1){
            //上一页
            map.put("prePage",1);

        }else{
            map.put("prePage",s.getPage()-1);

        }
        if(s.getPage()==sList.getPages()){
            //下一页
            map.put("nextPage",sList.getPages());

        }else{
            map.put("nextPage",s.getPage()+1);

        }
        //每页显示条数
        map.put("row",s.getRow());

        return map;
    }


    @Override
    public HashMap<String, Object> add(Supplier s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //新增
        int num = supplierMapper.insert(s);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> update(Supplier s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        int num = supplierMapper.updateById(s);
        if(num>0){
            map.put("info","保存成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> del(Integer s) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //删除
        int num =supplierMapper.deleteById(s);
        if(num>0){
            map.put("info","删除成功");
        }else{
            map.put("info","保存错误");
        }
        return map;
    }

}

